﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ecdl.demo.model.expression;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;

using System.Xml;
using System.Xml.Serialization;

namespace ecdl.demo.model.word {
    public class ParagraphExistsExpression : WordExpression {
        private const string POSITIVE_MESSAGE = "Der gesuchte Absatz wurde gefunden.";
        private const string NEGATIVE_MESSAGE = "Der gesuchte Absatz konnte nicht gefunden werden.";
        public String Regex { get; set; }

        protected override ExpressionState Evaluate(WordprocessingDocument document) {
            foreach (Paragraph p in GetAllParagraphs(document)) {
                string paragraphText = GetText(p);
                if (System.Text.RegularExpressions.Regex.IsMatch(paragraphText, Regex)) {
                    return Success(POSITIVE_MESSAGE, NEGATIVE_MESSAGE);
                }
            }
            return Fail(NEGATIVE_MESSAGE, POSITIVE_MESSAGE);
        }
    }

    public class ParagraphExistsExpressionDTO : ExpressionDTO {
        [XmlAttribute("regex")]
        public string Regex { get; set; }

        public override Expression ToModel(persistence.ModelFactory factory) {
            var expression = factory.CreateModel<ParagraphExistsExpression>();
            expression.Regex = Regex;
            return expression;
        }
    }
}
